package com.doctor.adapter;

import java.util.List;

import com.doctor.bean.PatientFileBean;
import com.doctor.ui.R;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PatientFileAdapter extends BaseAdapter{

	/**
	 * 患者档案adapter
	 */
	
	private Context context;
	private Click click;
	private HomeDoctorAdapterCallback callback;
	private List<PatientFileBean> beanList;
	
	public PatientFileAdapter(Context context,List<PatientFileBean> beanList,HomeDoctorAdapterCallback callback){
		this.context=context;
		this.beanList=beanList;
		click=new Click();
		this.callback=callback;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public PatientFileBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	public void clear(){
		beanList.clear();
	}
	
	public void removeBean(int position){
		beanList.remove(position);
		this.notifyDataSetChanged();
	}
	
	public boolean addBean(PatientFileBean bean){
		boolean result= beanList.add(bean);
		return result;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.patient_file_list_item, null);
			holder.number=(TextView) arg1.findViewById(R.id.number);
			holder.name=(TextView) arg1.findViewById(R.id.name);
			holder.address=(TextView) arg1.findViewById(R.id.address);
			holder.phoneBtn=(ImageView) arg1.findViewById(R.id.phoneBtn);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		PatientFileBean bean=getItem(arg0);
		
		holder.number.setText(bean.getId()+"");
		holder.name.setText(bean.getName());
		holder.address.setText(bean.getAddress());
		
		String phoneNumber=null;
		String mobile=bean.getMobile();
		String phone=bean.getPhone();
		if (mobile!=null && !"".equals(mobile.trim())) {
			phoneNumber=mobile;
		} else if (phone!=null && !"".equals(phone.trim())) {
			phoneNumber=phone;
		}
		holder.phoneBtn.setTag(R.id.phone_tag, phoneNumber);
		holder.phoneBtn.setOnClickListener(click);
		
		arg1.setTag(R.id.bean_tag,bean);
		return arg1;
	}

	private class ViewHolder{
		TextView number;
		TextView name;
		TextView address;
		ImageView phoneBtn;
	}
	
	public interface HomeDoctorAdapterCallback{
		public void onCallPhone(String phoneNumber);
	}
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			callback.onCallPhone((String) v.getTag(R.id.phone_tag));
		}		
	}
	
	
	
	
}
